package com.tencent.wxcloudrun.controller.fangdong;

import com.tencent.wxcloudrun.config.ApiResponse;
import com.tencent.wxcloudrun.config.RedisLock;
import com.tencent.wxcloudrun.pojo.dto.req.DealApplyReq;
import com.tencent.wxcloudrun.pojo.dto.resp.GetApplyAuthHouseListResp;
import com.tencent.wxcloudrun.pojo.dto.resp.GetApplyAuthListResp;
import com.tencent.wxcloudrun.pojo.dto.resp.ManageGetOneResp;
import com.tencent.wxcloudrun.pojo.dto.resp.ManagePageResp;
import com.tencent.wxcloudrun.service.business.fangdong.ManageService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import javax.validation.constraints.NotBlank;
import java.util.List;
import java.util.Map;

/**
 * 超级管理员
 */
@RestController
@RequestMapping("/manage")
@Validated
public class ManageController {

    @Resource
    private ManageService manageService;

    /**
     * 判断 是否有进入超管页面的权限
     * <p>
     * 用户点击【管理员】页签之后，弹窗提示输入密钥，然后调用这个接口看密钥对不对，不对的话不能进入管理员页面，
     * 对的话，跳转进管理员页面的时候要带上这个密钥，管理员相关的接口都是需要这个密钥的。<br>
     *
     * @return yes：验证通过；   no：不通过。
     */
    @GetMapping("power")
    public ApiResponse<String> power(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                     @RequestHeader(value = "X-Original-Forwarded-For", required = false) String clientIp) {
        return new ApiResponse<>(manageService.power(key, clientIp));
    }

    /**
     * 超管审批页的分页查询
     * <p>
     * 必须携带密钥，电话非必填，其实是个模糊查询来着，最多一次性可返回30条数据。
     */
    @GetMapping("getManagePage")
    public ApiResponse<List<ManagePageResp>> getManagePage(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                                           @RequestParam("tel") String tel) {
        return new ApiResponse<>(manageService.getManagePage(key, tel));
    }

    /**
     * 超管审批的详情页
     */
    @GetMapping("getOne")
    public ApiResponse<ManageGetOneResp> getOne(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                                @NotBlank(message = "房东ID不能为空") @RequestParam("landId") String landId) {
        return new ApiResponse<>(manageService.getOne(key, landId));
    }

    /**
     * 处理失败的审批情况 必须要带上失败的说明。
     */
    @GetMapping("dealFailApply")
    @RedisLock(prefix = "dealFailApply")
    public ApiResponse<String> dealFailApply(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                             @NotBlank(message = "房东ID不能为空") @RequestParam("landId") String landId,
                                             @NotBlank(message = "失败信息不能为空") @RequestParam("errInfo") String errInfo) {
        return new ApiResponse<>(manageService.dealFailApply(key, landId, errInfo));
    }

    /**
     * 处理成功的审批情况
     */
    @PostMapping("dealSuccessApply")
    @RedisLock(prefix = "dealSuccessApply")
    public ApiResponse<String> dealSuccessApply(@Valid @RequestBody DealApplyReq req) {
        return new ApiResponse<>(manageService.dealSuccessApply(req));
    }

    /**
     * 获取房屋认证的申请列表
     */
    @GetMapping("getApplyAuthList")
    public ApiResponse<List<GetApplyAuthListResp>> getApplyList(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key) {
        return new ApiResponse<>(manageService.getApplyAuthList(key));
    }

    /**
     * 获取申请人联系方式
     */
    @GetMapping("getApplyAuthTel")
    public ApiResponse<String> getApplyAuthTel(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                               @NotBlank(message = "认证ID不能为空") @RequestParam("authId") String authId) {
        return new ApiResponse<>(manageService.getApplyAuthTel(key, authId));
    }

    /**
     * 删除此认证申请
     */
    @GetMapping("deleteApplyAuth")
    public ApiResponse<String> deleteApplyAuth(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                               @NotBlank(message = "认证ID不能为空") @RequestParam("authId") String authId) {
        return new ApiResponse<>(manageService.deleteApplyAuth(key, authId));
    }

    /**
     * 进入房屋认证的房屋列表页面
     */
    @GetMapping("getApplyAuthHouseList")
    public ApiResponse<List<GetApplyAuthHouseListResp>> getApplyAuthHouseList(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                                                              @NotBlank(message = "认证ID不能为空") @RequestParam("authId") String authId) {
        return new ApiResponse<>(manageService.getApplyAuthHouseList(key, authId));
    }

    /**
     * 进行认证确认   result认证结果传yes或no
     */
    @GetMapping("dealApplyAuth")
    public ApiResponse<String> dealApplyAuth(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                             @NotBlank(message = "认证ID不能为空") @RequestParam("houseId") String houseId,
                                             @NotBlank(message = "认证结果不能为空") @RequestParam("result") String result) {
        return new ApiResponse<>(manageService.dealApplyAuth(key, houseId, result));
    }

    /**
     * 缓存清理接口
     */
    @PostMapping("clearCache")
    public ApiResponse<String> clearCache(@NotBlank(message = "权限密钥不能为空") @RequestParam("key") String key,
                                          @RequestBody Map<String, String> requestBody) {
        String cacheKey = requestBody.get("cacheKey");
        if (cacheKey == null || cacheKey.trim().isEmpty()) {
            throw new IllegalArgumentException("缓存键值不能为空");
        }
        return new ApiResponse<>(manageService.clearCache(key, cacheKey));
    }
}
